import time, Xml, os, os.path, subprocess,re
from time import gmtime, strftime

# subprocess.call, os.popen

def initialisationBackup(list):
    """ Copy the files to backup the first time into the backup directory"""
    
    backuppath=Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml','BackupDir')
    tempList=[]
    # Have to execute os.system in a different loop for eachotherwise get confused
    for elements in list:
       os.system('COPY /V /Y "'+elements+'" "'+backuppath[0]+'\\'+os.path.basename(elements)+'"')
    
    for elem in list:
        setAclBackup(os.path.basename(elem), os.path.dirname(elem))
        
    for el in list:
        xdeltaExtract(os.path.basename(el), os.path.dirname(el))
        tempList.append(os.path.basename(el))
    Btime(tempList)
    #Xml.addToXml('C:\\Program Files\\EasyBackupTool\\backupFile.xml', list)
    list = Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml', 'BackupDir')
    listBackup=os.listdir(list[0])
    listFinal=[]
    for elements in listBackup:
        pattern = '(.xdelta$|.acl$)'
        if (re.search(pattern, elements)):
            listFinal.append(elements)
    setHidden(listFinal)
    
def deleteFile(list):
    """ Delete certain files for cleanup """
    
    backuppath=Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml','BackupDir')
    
    for elements in list:
       os.system('DEL /AH "'+backuppath[0]+'\\'+os.path.basename(elements)+'"')
    for elements in list:
       os.system('DEL "'+backuppath[0]+'\\'+os.path.basename(elements)+'"')

def deleteFileH(list):
    """ Delete certain files for cleanup """
    
    backuppath=Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml','BackupDir')
    
    for elements in list:
       os.system('DEL /AH "'+backuppath[0]+'\\'+os.path.basename(elements)+'"')
        
def xdeltaExtract(fileName, pathtofile, levelOfCompression='9'):
    temp=[]
    xdeltapath=Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml','xdelta.exe')
    backuppath=Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml','BackupDir')
    execXDELTA = xdeltapath[0]
    oldFile = backuppath[0]+'\\'+fileName
    newFile = pathtofile+'\\'+fileName
    xdeltaFile = backuppath[0]+'\\'+fileName+'_'+time.strftime("%d-%m-%Y@%Hh%Mm%Ss")+'.xdelta'
    parameters = ' -'+levelOfCompression+' -S djw -e -vfs "'+oldFile.replace('\\', '/')+'" "'+newFile.replace('\\', '/')+'" "'+xdeltaFile.replace('\\','/')+'"'
    os.system('""' + execXDELTA + '"' + parameters)
    temp.append(fileName)
    Btime(temp)

def xdeltaRecover(date, fileName, pathtorecovery):
    xdeltapath=Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml','xdelta.exe')
    backuppath=Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml','BackupDir')
    execXDELTA = str(xdeltapath[0])
    oldFile = str(backuppath[0])+'\\'+str(fileName)
    deltaFile = str(backuppath[0])+'\\'+str(fileName)+'_'+str(date)+'.xdelta" "'
    recoveryDir = str(pathtorecovery)+'\\'+str(fileName)
    parameters = ' -d -vfs "'+oldFile.replace('\\', '/')+'" "'+deltaFile.replace('\\', '/')+recoveryDir.replace('\\', '/')+'"'
    #print '""' + execXDELTA + '"' + parameters
    os.system('""' + execXDELTA + '"' + parameters)

def setAclBackup(fileName,pathtofile):
    setaclpath=Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml','SetACL.exe')
    backuppath=Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml','BackupDir')
    execACL= str(setaclpath[0])
    target= str(pathtofile)+'\\'+str(fileName)
    destination= str(backuppath[0])+'\\'+str(fileName)
    parameters = r' -ot file -actn list -lst "f:sddl;w:d,s,o,g" -rec cont -bckp "'+destination.replace('\\', '/')+'_'+time.strftime("%d-%m-%Y@%Hh%Mm%Ss")+'.acl"'
    os.system ('""' +execACL+ '" -on "'+ target.replace('\\', '/') + '"' + parameters)
           
def setAclRestore(date,fileName,pathtofile):
    setaclpath=Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml','SetACL.exe')
    backuppath=Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml','BackupDir')
    execACL= str(setaclpath[0])
    target= str(pathtofile)+'\\'+str(fileName)
    aclFile = str(backuppath[0])+'\\'+str(fileName)+'_'+str(date)+'.acl"'
    parameters = ' -on "'+target.replace('\\', '/')+'" -ot file -actn restore -bckp "'+aclFile.replace('\\', '/')
    os.system('""' +execACL+ '"' + parameters)
    
def Backup():
    listToBackup = Xml.retrieveAllXml('C:\\Program Files\\EasyBackupTool\\tempFilesToBackup.xml')
    for elements in listToBackup:
        setAclBackup(os.path.basename(elements), os.path.dirname(elements))
    for elements in listToBackup:    
        xdeltaExtract(os.path.basename(elements), os.path.dirname(elements))
    list = Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml', 'BackupDir')
    listBackup=os.listdir(list[0])
    listFinal=[]
    for elements in listBackup:
        pattern = '(.xdelta$|.acl$)'
        if (re.search(pattern, elements)):
            listFinal.append(elements)
    setHidden(listFinal)

def Btime(list):
    lastbackups=Xml.retrieveAllXml('C:\\Program Files\\EasyBackupTool\\BackupTimes.xml')
    for elem in lastbackups:
        temp = elem.split('_')
        name = os.path.basename(temp[0])
        if name in list:
            lastbackups.remove(elem)
            list.remove(name)
            lastbackups.append(name+'_'+time.strftime("%d-%m-%Y@%Hh%Mm%Ss"))
    for elements in list:
        lastbackups.append(elements+'_'+time.strftime("%d-%m-%Y@%Hh%Mm%Ss"))
    Xml.createXmlBP('C:\\Program Files\\EasyBackupTool\\BackupTimes.xml','timesBackup',lastbackups)
    
def setHidden(list, sign='+'):
    backuppath=Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml','BackupDir')
    for elements in list:
        os.system('ATTRIB '+sign+'H "'+backuppath[0]+'\\'+elements+'"')
               
if __name__ == "__main__":
    import re
    
    list = Xml.retrieveInfoXml('C:\\Documents and Settings\\All Users\\Application Data\\EBT\\EBT-setup.xml', 'BackupDir')
    listBackup=os.listdir(list[0])
    listFinal=[]
    for elements in listBackup:
        pattern = '(.xdelta$|.acl$)'
        if (re.search(pattern, elements)):
            listFinal.append(elements)
    setHidden(listFinal)
    #setAclBackup('text.txt','C:\\Documents and Settings\\Feez\\Desktop\\')
    #xdeltaExtract('text.txt','C:\\Documents and Settings\\Feez\\Desktop\\')
    #xdeltaRecover('11-07-2008@14h05m53s', 'text.txt', 'C:\\')
    #setAclRestore('11-07-2008@14h36m13s', 'text.txt', 'C:\\')
    